package com.wen.moudle.Managers;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.wen.entry.Result;
import com.wen.entry.bean.Commodity;
import com.wen.moudle.Account;
import com.wen.moudle.abstractManager;

import java.util.List;

public class ShopManager extends abstractManager {
    public ShopManager(Account account) {
        super(account);
    }

    // 获取商品列表
    public Result<List<Commodity>> shopList(){
        HttpResponse response = HttpRequest.get(config.get("商品列表"))
                .cookie(getCookies())
                .execute();

        return JSONUtil.toBean(response.body(), new TypeReference<Result<List<Commodity>>>() {},false);
    }

    public Integer getCommodityId(String name){
        for (Commodity datum : shopList().getData()) {
            if (datum.getName().equals(name)){
                return datum.getId();
            }
        }
        return -1;
    }




    public Result<String> purchase(Integer commodityId, int num){
        HttpResponse response = HttpRequest.post(config.get("购买")+commodityId + "/" + num)
                .cookie(getCookies())
                .execute();
        return JSONUtil.toBean(response.body(), new TypeReference<Result<String>>() {},false);
    }
}
